package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureSettingsDAO;
import org.jetnuke.entities.PictureSettings;

public class GenericPictureSettingsDAO implements
		PictureSettingsDAO {

	public PictureSettings get() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureSettingsModel.get"));
		
		ResultSet rs = p.executeQuery();
		PictureSettings s = new PictureSettings();
		
		rs.next();
		fillPictureSettingsFromResultSet(s, rs);
		
		rs.close();
		p.close();

		return s;
	}

	private void fillPictureSettingsFromResultSet(PictureSettings s, ResultSet rs)throws Exception {
		s.setLICENSE(rs.getString("PICTURE_LICENSE_ACCEPT"));
		s.setLIMIT(rs.getInt("SIZE_LIMIT_KB"));
	}

	public void update(PictureSettings bs) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureSettingsModel.update"));
		p.setString(1,bs.getLICENSE());
		p.setInt(2,bs.getLIMIT());
		p.executeUpdate();
		p.close();

	}

}
